<?php
/**
 * --------------------------------------------------------------------
 * 节点控制器
 * --------------------------------------------------------------------
 * @author Midnight, yangyunzhou@foxmail.com
 * --------------------------------------------------------------------
 * @copyright Midnight Studio
 * --------------------------------------------------------------------
 */
class NodeAction extends AdminBaseAction {

	/**
	 * --------------------------------------------
	 * 节点管理
	 * --------------------------------------------
	 * @author Midnight, yangyunzhou@foxmail.com
	 */
	public function manage() {
		$this->title = '节点管理';
		$db = D('Node');
		$pid = $_GET['pid'] ? $_GET['pid'] : 0;
		$this->list = $db->where("`pid`='$pid'")->select();
		$this->display('Admin:node/manage');
	}

	/**
	 * --------------------------------------------
	 * 添加节点
	 * --------------------------------------------
	 * @author Midnight, yangyunzhou@foxmail.com
	 */
	public function add() {
		if($this->isFormPost() && $this->isValidToken()) {
			$db = D('Node');
			$action = $db->nodeAction($_GET['pid']);
			$_POST['level'] = $action['level'];
			$_POST['pid'] = $action['id'];
			//dump($db->create());
			//exit();
			if(!$db->create()) $this->error($db->getError());
			if($db->add()) $this->success('添加成功！');
			else $this->error('添加失败！');
		} else {
			$this->title = '节点添加';
			$this->display('Admin:node/add');
		}
	}

	/**
	 * --------------------------------------------
	 * 编辑节点
	 * --------------------------------------------
	 * @author Midnight, yangyunzhou@foxmail.com
	 */
	public function edit() {
		if($this->isFormPost() && $this->isValidToken()) {
			$db = D('Node');
			$action = $db->nodeAction($_GET['pid']);
			$_POST['level'] = $action['level'];
			$_POST['pid'] = $action['id'];
			if(!$db->create()) $this->error($db->getError());
			$db->{$db->pk} = $_GET['id'];
			if($db->save()) $this->success('修改成功！');
			else $this->success('没有任何修改！');
		} else {
			$this->set_referer();
			$this->title = '编辑节点';
			$this->val = D('Node')->get_data($_GET['id']);
			$this->display('Admin:node/edit');
		}
	}

	/**
	 * --------------------------------------------
	 * 删除节点
	 * --------------------------------------------
	 * @author Midnight, yangyunzhou@foxmail.com
	 */
	public function delete() {}
}